﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;

namespace SqlServerDataTableToExcel.DBhelper
{
    public class MySqlDbhelper
    {
        private string connStr { get; set; }

        public MySqlDbhelper(string IPAddress, string DataSource, string UserName, string Pwd)
        {
            if (string.IsNullOrEmpty(IPAddress))
            {
                throw new Exception("服务器IP地址不能为空！");
            }
            connStr = string.Format("server={0};Database={1};User Id={2};password={3};Charset=utf8", IPAddress, DataSource, UserName, Pwd);
        }


        public  DataTable GetUsrDataSource(string sql )
        {
            if (string.IsNullOrWhiteSpace(connStr))
            {
                return null;
            }
            MySqlConnection conn = new MySqlConnection(connStr);
            MySqlDataAdapter da = new MySqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}
